const koa = require('koa');
const app = new koa();
const render = require('koa-art-template');
const router = require('koa-router')();
const path = require('path')
const DB = require('./modules/db');
const bodyParser = require('koa-bodyparser');
app.use(bodyParser());
render(app, {
    root: path.join(__dirname, 'views'),
    extname: '.html', //后缀也可以写成.art
    debug: process.env.NODE_ENV !== 'production'
});
router.get('/', async ctx => {
    var data = await DB.find("user", {});
    console.log(data);
    await ctx.render('index', {
        data
    })
})
router.get('/detail', async ctx => {
    await ctx.render("detail", {
        detail: "详情"
    })
})
router.get('/add',async ctx=>{
    await ctx.render('add')
})
router.post('/doAdd',async ctx=>{
  var data = await DB.insert("user", ctx.request.body);
  try{
      if(data.result.ok){
          ctx.redirect('/')
      }
  }catch(err){
      console.log(err)
      ctx.redirect('/add')
  }
})
app.use(router.routes()).use(router.allowedMethods())
app.listen(8080)